from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import sys
import os

from engines.webengine.YAWKWebEngine import *
from engines.webkit.YAWKWebKit import *
from engines.EngineView import EngineAdapter
from ui.settings.settingstab import SettingsTab

class YawkWindow(QMainWindow):
    index = 0
    settingTabOpen = False

    def __init__(self, *args, **kwargs):
        super(YawkWindow, self).__init__(*args, **kwargs)

        self.setStyleSheet("* {"
            "font-family: -apple-system, BlinkMacSystemFont, 'BreezeSans, 'Product Sans', 'Google Sans', 'SF Pro Display', 'Segoe UI', 'Arial', sans-serif;"
        "}")

        self.resize(1280, 720)

        self.mainWidget = QWidget()
        self.mainWidget.layout = QVBoxLayout(self)
        self.mainWidget.layout.setContentsMargins(0, 0, 0, 0)
        self.mainWidget.setLayout(self.mainWidget.layout)

        self.tabs = QTabWidget()
        self.tabs.setDocumentMode(True)
        self.tabs.setTabsClosable(True)
        self.tabs.tabCloseRequested.connect(self.closeTab)

        self.mainWidget.layout.addWidget(self.tabs)

        self.setCentralWidget(self.mainWidget)

        self.show()

        #self.add(EngineAdapter(self, YawkWebEngine(self)), "New tab.")

        self.setWindowTitle("yawk.")
        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())

        self.bottomBar = QWidget()
        self.bottomBar.setStyleSheet("QWidget {padding: 0px; height: 24px; max-height: 26px; width: auto;}")
        self.bottomBar.layout = QHBoxLayout(self)
        self.bottomBar.layout.setContentsMargins(0, 0, 0, 0)
        self.bottomBar.setLayout(self.bottomBar.layout)
        self.mainWidget.layout.addWidget(self.bottomBar)

        """
        self.addTabButton = QToolButton()
        self.addTabButton.setText("+  ")
        self.addTabButton.setStyleSheet("QToolButton {border-radius: 1px; border: 1px solid gray; background-color: gray;} "
                                        "QMenuBar {height: 400px;}"
                                        "QMenu {width: 200px; padding: -3px; border: 1px solid gray; background-color: gray;}")
        self.addTabButton.setPopupMode(QToolButton.MenuButtonPopup)
        self.addRegularTab = QAction("Create a new tab.")
        self.addSafeTab = QAction("Create a safe mode tab.")
        self.addTorTab = QAction("Create a torified tab.")
        self.addTabButton.addActions([self.addRegularTab, self.addSafeTab, self.addTorTab])
        self.addTabButton.defaultAction = self.addRegularTab
        self.addTabButton.setToolTip("Add Tab: Creates a brand new tab.")
        #self.addTabButton.clicked.connect(self.genericAdd)
        self.bottomBar.layout.addWidget(self.addTabButton, Qt.AlignLeft)
        """  # TODO: Find out why only 1 of 3 actions is added.

        self.regularAdd = QPushButton()
        self.regularAdd.setText("+")
        self.regularAdd.setToolTip("Add Tab: Creates a brand new tab.")
        self.regularAdd.clicked.connect(self.genericAdd)
        self.bottomBar.layout.addWidget(self.regularAdd, Qt.AlignLeft)

        self.safeModeAdd = QPushButton()
        self.safeModeAdd.setToolTip("Add Safe Tab: Creates a one time secure tab.")
        self.safeModeAdd.setText("S+")

        self.bottomBar.layout.addWidget(self.safeModeAdd, Qt.AlignLeft)


        self.spacer = QSpacerItem(4096, 1, QSizePolicy.Maximum, QSizePolicy.Ignored)
        self.bottomBar.layout.addSpacerItem(self.spacer)

        self.settingsButton = QPushButton()
        self.settingsButton.setToolTip("Settings: Opens up the YAWK Settings UI.")
        self.settingsButton.setText("☰")
        self.settingsButton.clicked.connect(self.settingsAdd)
        self.settingsButton.setStyleSheet("QPushButton {padding: 0px; height: 26px; max-height: 30px; width: 40px; max-width: 40px;}")
        self.bottomBar.layout.addWidget(self.settingsButton, Qt.AlignRight)

    def add(self, tab, tabName="New tab."):
        self.tabs.addTab(tab, tabName)
        self.index += 1

    def getIndex(self):
        return self.index

    def setTabText(self, index, text="Generic Tab."):
        self.tabs.setTabText(index, text)

    def genericAdd(self):
        read = getSetting("sys.browser_mode", 0)
        if read == 0:
            self.add(EngineAdapter(self, YawkWebEngine(self)), "New tab.")
        else:
            self.add(EngineAdapter(self, YawkWebKit(self)), "New tab.")

    def settingsAdd(self):
        if not self.settingTabOpen:
            self.settingTabOpen = True
            self.add(SettingsTab(self), "Settings.")

    def closeTab(self, index):
        widget = self.tabs.widget(index)
        if type(widget) is SettingsTab:
            self.settingTabOpen = False
        elif type(widget) is EngineAdapter:
            widget.makeClose()
        self.index -= 1
        self.tabs.removeTab(index)
        widget.close()
        widget.destroy(False, True)

app = QApplication(sys.argv)
window = YawkWindow()
app.exec_()